import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login'
import flights_monitor from '../views/flights_monitor'
import flights_scheduled_info from '../views/flights_scheduled_info'
import airport_weather_monitor from '../views/airport_weather_monitor'
import runway_monitor from '../views/runway_monitor'
import airport_security_monitor from '../views/airport_security_monitor'
import flights_manage from '../views/flights_manage'

// 测试用导入当vue组件
import radar from '../components/flights_monitor/Radar'
import Scheduled_entry_flights from '../components/flights_monitor/Scheduled_entry_flights'
import Clock from '../components/flights_monitor/Clock'

Vue.use(VueRouter)

const routes = [
  // 路由重定向
  { path: '/', redirect: '/login' },
  // 注册login路由
  { path: '/login', component: login },
  // 注册在飞航班航迹监控页面
  { path: '/flights_monitor', component: flights_monitor },
  // 注册航班信息监控页面路由
  { path: '/flights_scheduled_info', component: flights_scheduled_info },
  // 注册机场天气监控页面路由
  { path: '/airport_weather', component: airport_weather_monitor },
  // 注册跑道监控页面路由
  { path: '/runway', component: runway_monitor },
  // 注册机场安防监控页面路由
  { path: '/airport_security', component: airport_security_monitor },
  // 注册航班管理页面路由
  { path: '/flights_manage', component: flights_manage },


  //测试组件
  { path: '/radar', component: radar },
  { path: '/Scheduled_entry_flights', component: Scheduled_entry_flights },
  { path: '/Clock', component: Clock },


]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
/*
*  to : 代表将要访问到路径
*  from : 代表从哪个路径跳转而来
*  next : next 函数，代表放行。 两种写法：1、next() 放行 2、next('/login') 表示强制跳转到路径
*/
router.beforeEach((to, from, next) => {
  if (to.path === '/login'){
    return next();
  }
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr){
    return next('/login');
  }else {
    return next();
  }
})

export default router
